//
//  THJMyQiugouVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/11/23.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJMyQiugouVC.h"
#import "THJTAdeFabuCell.h"
#import "ZBNetworking.h"
#import "THJMyQiuGouDetailVC.h"
#import <MJRefresh.h>

@interface THJMyQiugouVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger pageNo;
    UITableView * _tableview;
}
@property(nonatomic,strong)NSMutableArray * dataArr;
@end

@implementation THJMyQiugouVC
-(NSMutableArray*)dataArr{
    if(!_dataArr){
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    [self.dataArr removeAllObjects];
    [self getDataWithPage:1];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    pageNo = 1;
    if([self.qiugouType isEqual:@"buyer"]){
        if(KIsiPhoneX){
            [self setXNavBarWithTitle:@"我的求购"];
        }else{
            [self setNavBarWithTitle:@"我的求购"];
        }
    
    }else{
        if(KIsiPhoneX){
            [self setXNavBarWithTitle:@"他人求购"];
        }else{
            [self setNavBarWithTitle:@"他人求购"];
        }
    }
    
}
-(void)getDataWithPage:(NSInteger)page{
    __weak typeof(self) weekself = self;
    NSString * urlstr =  [NSString stringWithFormat:@"%@index.php?f=My_buy_6&r=My_buy_6&m=index&type=%@&page=%ld",THJUrlDomain,self.qiugouType,page];
    NSLog(@"我的求购url-%@",urlstr);
    NSString * token = [THJTools getToken];
//    [MBProgressHUD showMessage:@"" toView:self.view];
    
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString=urlstr;
        request.apiType=ZBRequestTypeRefresh;
        [request setValue:token forHeaderField:@"token"];
    }  success:^(id responseObject,apiType type){
        [_tableview.mj_footer endRefreshing];
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
//        [MBProgressHUD hideHUDForView:weekself.view animated:YES];
        NSLog(@"我的求购 -%@",receiveData);
        if([receiveData[@"status"] isEqual:@"200"]){
            for (NSDictionary * dic in receiveData[@"data"]) {
                [weekself.dataArr addObject:dic];
            }
          _tableview = [weekself setTableviewWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-NavbarHeight) RegisterNibNameArr:@[@"THJTAdeFabuCell"] ReuseIdentifierArr:@[@"cell"] RowHeight:145 BgColor:SingleColor(whiteColor) IsLoadMore:YES];
            if ([receiveData[@"nexpage"] isEqual:@"end"]){
                [_tableview.mj_footer endRefreshingWithNoMoreData];
            }
        }else if(![receiveData[@"status"] isEqual:@"200"]){
            [weekself HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
            [weekself performSelector:@selector(pop) withObject:nil afterDelay:1];
        }
    } failed:^(NSError *error){
        [_tableview.mj_footer endRefreshing];
//        [MBProgressHUD hideHUDForView:weekself.view animated:YES];
        NSLog(@"error-%@",error);
    }];
}
-(void)loadMore{
    pageNo++;
    [self getDataWithPage:pageNo];
    [_tableview.mj_footer endRefreshing];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dic = self.dataArr[indexPath.row];
    THJTAdeFabuCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLab.text = dic[@"title"];
    cell.addressLab.text = dic[@"address"];
    cell.companLab.text = dic[@"company"];
    cell.priceLab.text = dic[@"price"];
    cell.dateLab.text = dic[@"adddate"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if([self.qiugouType isEqual:@"buyer"]){
        NSDictionary * dic = self.dataArr[indexPath.row];
        THJMyQiuGouDetailVC * vc = [[THJMyQiuGouDetailVC alloc] init];
        vc.qiugouType = self.qiugouType;
        vc.itemid = dic[@"itemid"];
        [self.navigationController pushViewController:vc animated:YES];
        NSLog(@"dianjicell--");
    }
}
@end
